package com.own.business.question.common.handler;

import com.own.business.question.common.base.ReplyEnvironment;
import com.own.business.question.common.constant.ConstantReplyEnvironment;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * ReplyEnvironmentHandler
 *
 * @author chenxueli
 * @date 2021/5/14 15:31
 */
public class ReplyEnvironmentHandler implements TypeHandler<ReplyEnvironment> {

    @Override
    public void setParameter(PreparedStatement ps, int i, ReplyEnvironment environment, JdbcType jdbcType) throws SQLException {
        ps.setInt(i, environment.type());
    }

    @Override
    public ReplyEnvironment getResult(ResultSet rs, String columnName) throws SQLException {
        int result = rs.getInt(columnName);
        return ConstantReplyEnvironment.get(result);
    }

    @Override
    public ReplyEnvironment getResult(ResultSet rs, int i) throws SQLException {
        int result = rs.getInt(i);
        return ConstantReplyEnvironment.get(result);
    }

    @Override
    public ReplyEnvironment getResult(CallableStatement cs, int i) throws SQLException {
        int result = cs.getInt(i);
        return ConstantReplyEnvironment.get(result);
    }

}
